package org.client.otdr.executor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.client.otdr.Executor;
import org.client.otdr.OTDRID;
import org.openems.BookmarkEntities;
import org.openems.BookmarkEntity;
import org.openems.entity.ChoiceEntity;
import org.openems.entity.Key;

import framework.Accessor;
import framework.webclient.RestSocket;
import framework.webclient.SystemProperty;

public class SavenameExecutor implements Executor {

	private Accessor accessor;
	private RestSocket restSocket;
	private SystemProperty systemProperty;
	
	public SavenameExecutor(Accessor accessor, RestSocket restSocket, SystemProperty systemProperty) {
		this.accessor = accessor;
		this.restSocket = restSocket;
		this.systemProperty = systemProperty;
	}

	public void execute(Key key) {
		OTDRID id = OTDRID.get();
		String bookmarkName = accessor.get(id.SAVENAME).getValueText();
		BookmarkEntity entity = new BookmarkEntity();
		entity.bookmarkName = bookmarkName;
		entity.productName = systemProperty.getProductName();
		restSocket.put("bookmark", entity, String.class);
		
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("productName", entity.productName);
		BookmarkEntities entities = restSocket.get("bookmark", BookmarkEntities.class, parameters);
		List<String> list = new ArrayList<String>();
		for (BookmarkEntity e : entities.entities) {
			list.add(e.bookmarkName);
		}
		ChoiceEntity choiceEntity = ((ChoiceEntity)accessor.get(id.BOOKMARK));
		choiceEntity.setChoices(list);
		accessor.put(choiceEntity);
	}

}
